#!/bin/sh

DATA_DIR=$1
ALGS=`ls $1 | grep edat | cut -d. -f2 | sort | uniq`
Benches=`ls $1 | grep edat | cut -d. -f3 | sort | uniq`
Threads=`ls $1 | grep edat | cut -d. -f4 | sort -n | uniq`
REP=3
count=1
sum=0
tmp=0

line="#bm threads"
for ALG in $ALGS
do
	count=1 
	
	while [ $count -le $REP ]
	do
		line="$line $ALG"
		count=`expr $count + 1`
	done
	
	line="$line ${ALG}AVG"
done

echo $line

for Bench in $Benches
do
	if [ "$Bench"x = "ssc"x ]; then
		for Thread in $Threads
		do
			line="$Bench $Thread"
			
			for ALG in $ALGS
			do
				count=1
				sum=0
				tmp=0
				available_count=$REP
				while [ $count -le $REP ] 
				do 
#					echo "catting ${DATA_DIR}NONE.$ALG.ssc.$Thread.$count.32.edat"
					tmp=`cat ${DATA_DIR}/NONE.$ALG.ssc.$Thread.$count.32.edat | grep kernel | awk '{print $7}'`
					if [ "$tmp"x = ""x ]; then
#						echo "NULL value from file ${DATA_DIR}NONE.$ALG.ssc.$Thread.$count.32.edat!"
						line="$line N/A"
						available_count=`expr $available_count - 1`
					else
						line="$line $tmp"
						sum=`echo "$sum + $tmp" | bc`
					fi

					count=`expr $count + 1`

				done
			
				line="$line `echo \"scale=6;$sum/$available_count\" | bc`"
			done

			echo $line
		done
	else
	
		for Thread in $Threads
		do
			line="$Bench $Thread"
			
			for ALG in $ALGS
			do
				count=1
				sum=0
				tmp=0
				available_count=$REP
				while [ $count -le $REP ]
				do
#					echo "catting ${DATA_DIR}NONE.$ALG.$Bench.$Thread.$count.32.edat" 
					tmp=`cat ${DATA_DIR}/NONE.$ALG.$Bench.$Thread.$count.32.edat | grep Time | awk '{print $3}'`
					if [ "$tmp"x = ""x ]; then
#						echo "NULL value from file ${DATA_DIR}NONE.$ALG.ssc.$Thread.$count.32.edat!"
						line="$line N/A"
						available_count=`expr $available_count - 1`
					else
						line="$line $tmp"
						sum=`echo "$sum + $tmp" | bc`
					fi
					
					count=`expr $count + 1`

				done
	
				line="$line `echo \"scale=6;$sum/$available_count\" | bc`"
			done
			
			echo $line
		done

	fi
done

